package com.photoeditor.demo.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.common.base.ComponentContext;
import com.common.base.log.RLog;
import com.common.base.utils.ProcessUtils;
import com.photoeditor.R;
import com.photoeditor.demo.model.preference.SPDataManager;
import com.photoeditor.demo.model.preference.SpUtils;
import com.photoeditor.demo.model.subscribe.manager.SVipConstant;
import com.photoeditor.demo.model.subscribe.manager.SVipController;
import com.photoeditor.demo.statistics.StatisticsConstant;
import com.photoeditor.demo.statistics.StatisticsUtils;

import java.lang.ref.WeakReference;


//public class SplashActivity extends Activity implements BaseOnlineManager.StoreOnlineListener {
public class StartActivity extends Activity {

    private static final int MESSAGE_JUMP_MAIN = 111;
    private static final long DELAY = 500;
    private SplashHandler mHandler = new SplashHandler(StartActivity.this);

//    private boolean mFirstAddStoreListener = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        setContentView(R.layout.activity_splash);

        mHandler.sendEmptyMessageDelayed(MESSAGE_JUMP_MAIN, DELAY);

        SPDataManager.setOpenAppCountToday(SPDataManager.getOpenAppCountToday() + 1);

        if (!SpUtils.obtain().getBoolean("st_n_fs", false)) {
            SpUtils.obtain().save("st_n_fs", true);
            StatisticsUtils.statisticGrowingIo(StatisticsConstant.EVENT_NEW_FLASH);
        }
        boolean mainProcess = ProcessUtils.isMainProcess(ComponentContext.getContext());
        Log.v("growing", "test flash， current is main process:" + mainProcess);
        RLog.v("growing", "test flash， current is main process:" + mainProcess);
        StatisticsUtils.statisticGrowingIo("test_flash");

        SpUtils.obtain().save("startup_count", SpUtils.obtain().getInt("startup_count", 0) + 1);

//        if (SPDataManager.hasCheckPrivacy()) {
//            jumpToMain();
//        } else {
//            showPrivacyCheckView();
//            addStoreListener();
//        }

    }

//    private void showPrivacyCheckView() {
//        statisicsCustomFunction(StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_SPLASH_PRIVACY,
//                StatisticsConstant.UMENG_CUSTOM_EVENT_KEY_SHOW, StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_SPLASH_PRIVACY_DISPLAY);
//        ViewStub viewStub = findViewById(R.id.viewstub_privacy_check);
//        PrivacyCheckView privacyCheckView = (PrivacyCheckView) viewStub.inflate();
//
//        privacyCheckView.setOnPrivacyViewClickListener(new PrivacyCheckView.OnPrivacyViewClickListener() {
//            @Override
//            public void onCheckBtnClick() {
//                SPDataManager.setHasCheckPrivacy();
//                jumpToMain();
//                callViewDetail(ViewDetailActivity.SPLASH_APPLY);
//                statisicsCustomFunction(StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_SPLASH_PRIVACY,
//                        StatisticsConstant.UMENG_CUSTOM_EVENT_KEY_CLICK, StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_SPLASH_PRIVACY_CONFIRM);
//            }
//        });
//    }


    private void jumpToMain() {
        if (!SVipController.showSubscribeActivityFromSplash(this, SVipConstant.VIP_ENTRANCE_IN_APP)) {
            Intent i = new Intent(this, HomeActivity.class);
            startActivity(i);
        }
        overridePendingTransition(R.anim.activity_no_animation, R.anim.activity_close_exit);
        finish();
    }

//    private void addStoreListener() {
//        StoreOnlineBean bannerBean = HomeOnlineManager.getInstance().getBannerBean();
//        if (bannerBean == null || bannerBean.getChildModules() == null || bannerBean.getChildModules().size() == 0) {
//            if (mFirstAddStoreListener) {
//                mFirstAddStoreListener = false;
//                HomeOnlineManager.getInstance().addDataListener(this);
//            }
//        }
//    }

    /**
     * 下载第一张banner的大图，用于闪屏显示使用
     */
//    private void loadFirstBannerImgFile() {
//        StoreOnlineBean bannerBean = HomeOnlineManager.getInstance().getBannerBean();
//        if (bannerBean != null && bannerBean.getChildModules() != null && bannerBean.getChildModules().size() > 0) {
//            StoreOnlineBean bean = bannerBean.getChildModules().get(0);
//            if (bean.getChildModules() != null && bean.getChildModules().size() > 0) {
//                String[] urls = bean.getChildModules().get(0).getPreview().split("#");
//                if (urls.length > 0) {
//                    final String url = urls[0];
//                    ThreadPool.runOnNonUIThread(new Runnable() {
//                        @Override
//                        public void run() {
//                            ImageLoader.downloadImageFile(ComponentContext.getContext(), url);
//                        }
//                    });
//                }
//            }
//        }
//    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeMessages(MESSAGE_JUMP_MAIN);
//        HomeOnlineManager.getInstance().removeListener(this);
    }

//    @Override
//    public void needUpdateData(boolean needUpdate) {
//        loadFirstBannerImgFile();
//    }

    private static class SplashHandler extends Handler {
        private final WeakReference<StartActivity> mSplashActivityRef;

        private SplashHandler(StartActivity mSplashActivityRef) {
            this.mSplashActivityRef = new WeakReference<>(mSplashActivityRef);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            StartActivity splashActivity = mSplashActivityRef.get();
            if (splashActivity != null && msg.what == MESSAGE_JUMP_MAIN) {
                splashActivity.jumpToMain();
            }
        }
    }

}
